import {request} from "../../request/index.js"
Page({
  data:{
    // address:'',
    defaultAddress:'',
    cart:'',
    totalPrice:0,
    totalNum:0
  },
  onShow(){
    let that = this;
    // 获取缓存中收货地址
    // wx.getStorage({
    //   key: "address",
    //   success: function(res){
    //     that.setData({
    //       address:res.data
    //     })
    //   }
    // });
    // 地址 要么是默认 要么是第一个
    let userAddress = wx.getStorageSync("userAddress");
    let defaultAddress = userAddress.find(v=>v.add_check=='true');
      if(defaultAddress){
        // 如果有默认地址
        that.setData({
          defaultAddress
        })
      }else{
        // 如果没设默认地址 以第一个地址为默认地址
        that.setData({
          defaultAddress:userAddress[0]
        })
      }
    // 获取缓存中的购物车数据
    let cart = wx.getStorageSync("cart")||[];
    // 过滤后的购物车数组 即只筛选选择的商品
    cart = cart.filter(v=>v.checked);
    let totalPrice=0;
    let totalNum=0;
    cart.forEach(v => {
      totalPrice+=v.num*v.price;
      totalNum+=v.num;
    });
    this.setData({
       cart,
       totalPrice,
       totalNum
     })
  },
  // 点击支付
  async handleOrderPay(){
    // 1、发请求或从缓存中获取token
    const token = wx.getStorageSync("token");
    if(!token){
      const nickName = wx.getStorageSync("nickName")
      const token = await request({url:"/getToken",method:'Post',data:{nickName}})
      wx.setStorageSync('token', token.data.token)
    }
    // 2、发请求生成订单 获取订单编号order_number
    // 准备请求头参数
    const header = {token:token};
    // 准备请求体参数
    const order_price=this.data.totalPrice;
    // const order_addr=this.data.address.provinceName+this.data.address.cityName+this.data.address.countyName+this.data.address.detailInfo;
    const order_addr=this.data.add_area+this.data.add_detail;
    const cart = this.data.cart;
    let goods=[];
    cart.forEach(v=>goods.push({
      id:v.id,
      num:v.num,
      price:v.price
    }))
    const orderParams={order_price,order_addr,goods};
    // 发请求 创建订单存入数据库 得到订单编码
    const res=await request({url:"/creatOrder",method:"POST",data:orderParams,header});
    // console.log(res.data.order_number);
    let order_number=res.data.order_number;
    // 3、通过token与order_num发请求 得到内置API支付需要的参数 个人无法调用 略过
    // const res1 = await request({url:'/getPayParams',method:'POST',header,data:{order_number}})
    // 4、调用支付API 略 自己想办法把支付状态改为true
    if (res.data.order_number) {
      // 弹窗问是否支付
      wx.showModal({
        title: '是否支付',
        content: '',
        success: function(res) {
          if(res.confirm){
            // 确认支付则跳转到truePay页面，将订单支付状态改为true
            // wx.navigateTo({
              
            //   // url: `../truePay/truePay?order_number=$(order_number)`
            //   url: '/pages/truePay/truePay'
            // })
            wx.navigateTo({
              // url: '/pages/truePay/truePay'
              url: '/pages/truePay/truePay?order_number='+order_number
            })
          }else{
            // 否 提示取消支付
            console.log('取消支付');
          }
        }
      })
        
    }
    // 5、删除缓存中  已购买的商品 也可不用删除

  }
})